#include <QGuiApplication>
#include <QQmlApplicationEngine>


#include "person.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);

    //    //注入一个类型到qml里面
    qmlRegisterType<GtPerson>("gtModule", 1, 0, "GtPerson");
    qmlRegisterType<GtJob>("gtModule", 1, 0, "GtJob");

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/9.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    engine.load(url);

    return app.exec();
}


//#include "gtcell.h"
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    //    //注入一个类型到qml里面
//    qmlRegisterType<GtCell>("gtModule", 1, 0, "GtCell");

//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/8.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);

//    engine.load(url);

//    return app.exec();
//}

//注入一个类型到qml中并使用附加属性
//#include "person.h"
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    //    //注入一个类型到qml里面
//    qmlRegisterUncreatableType<PersonFor7>("gtModule", 1, 0, "PersonFor7", "gt:不允许创建实例");

//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/7.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);

//    engine.load(url);

//    return app.exec();
//}

//注入一个类型到qml里面
//#include "person.h"
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    //注入一个类型到qml里面
//    qmlRegisterType<Info>("gtModule", 1, 0, "Info");



////        另一种方法(文档中查询QML_ELEMENT，此方法代替上面那一句话注册)
////        persion.h Info类中
////        Q_PROPERTY(QDate date READ getDate WRITE setDate NOTIFY dateChanged)
////        下面写上
////        QML_ELEMENT

////        .pro文件顶上加上
////        CONFIG += qmltypes
////        QML_IMPORT_NAME = gtModule
////        QML_IMPORT_MAJOR_VERSION = 1

////        此种方法会多一个元对象数据json文件




//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/6.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);

//    engine.load(url);

//    return app.exec();
//}


//cpp信号触发qml的槽函数
//#include "person.h"
//#include <QQmlContext>
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/5.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);

//    Person person("gt", 18);
//    auto context = engine.rootContext();
//    context->setContextProperty("Person", &person);

//    engine.load(url);

//    return app.exec();
//}


//cpp与qml信号绑定，qml触发cpp的槽函数
//#include "person.h"
//#include <QQmlContext>
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/4.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);
//    engine.load(url);

//    //我c++要调用qml的函数
//    Person person("gt", 18);

//    auto root = engine.rootObjects();
//    auto buttonQml = root.first()->findChild<QObject*>("buttonQml");

//    QObject::connect(buttonQml, SIGNAL(clicked()), &person, SLOT(clickDemo()));
//    QObject::connect(buttonQml, SIGNAL(countDemo(int)), &person, SLOT(countDemo(int)));

//    return app.exec();
//}



//qml调用cpp, 注册单例绑定
//#include "person.h"
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    //qml调用cpp, 注册单例绑定
//    Person person("gt", 18);
//    qmlRegisterSingletonInstance("gtModule", 1, 0, "Person", &person);

//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/3.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);
//    engine.load(url);
//    return app.exec();
//}




//qml调用cpp 使用最多的！！！！注入一个实例
//使用engine.rootContext().setContextProperty
//#include "person.h"
//#include <QQmlContext>
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/2.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);
//    engine.load(url);

//    Person person("gt", 18);
//    auto context = engine.rootContext();
//    context->setContextProperty("Person", &person);

//    return app.exec();
//}




//cpp调用qml
//#include <QMetaObject>
//int main(int argc, char *argv[])
//{
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
//    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//#endif
//    QGuiApplication app(argc, argv);

//    QQmlApplicationEngine engine;
//    const QUrl url(QStringLiteral("qrc:/1.qml"));
//    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
//                     &app, [url](QObject *obj, const QUrl &objUrl) {
//        if (!obj && url == objUrl)
//            QCoreApplication::exit(-1);
//    }, Qt::QueuedConnection);
//    engine.load(url);


//    //我c++要调用qml的函数
//    auto root = engine.rootObjects();
//    auto labelQml = root.first()->findChild<QObject*>("labelQml");
//    QVariant ret;

//    //无参数
//    //QMetaObject::invokeMethod(labelQml, "getText", Q_RETURN_ARG(QVariant, ret));
//    //有参数
//    QMetaObject::invokeMethod(labelQml, "getText", Q_RETURN_ARG(QVariant, ret), Q_ARG(QVariant, " jie"));
//    qDebug() << ret.toString();

//    return app.exec();
//}






